package 状态机.请假demo.handler;

import 状态机.请假demo.leavepermit.LeavePermitType;
import 状态机.请假demo.status.Status;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 注册请假类型以及该假的处理者
 */
public class StatusHandlerRegistry {

    private static Map<String, IStatusHandler> statusHandlerMap;

    static {
        statusHandlerMap = new ConcurrentHashMap<String, IStatusHandler>();
    }

    private StatusHandlerRegistry() {

    }

    private static String getKey(LeavePermitType leavePermitType, Status status) {
        return String.format("%s@-@%s", leavePermitType.getType(), status.name());
    }

    /**
     * 注册状态处理类
     *
     * @param leavePermitType 请假类型
     * @param status          请假状态
     * @param statusHandler   状态处理对象
     */
    public static void registryStatusHandler(LeavePermitType leavePermitType, Status status, IStatusHandler statusHandler) {
        statusHandlerMap.put(getKey(leavePermitType, status), statusHandler);
    }

    /**
     * 获取状态处理类
     *
     * @param leavePermitType 请假类型
     * @param status          请假状态
     * @return StatusHandler
     */
    public static IStatusHandler acquireStatusHandler(LeavePermitType leavePermitType, Status status) {
        return statusHandlerMap.get(getKey(leavePermitType, status));
    }

}
